#!/usr/bin/python

from gnuradio import gr
from gnuradio.gr import firdes
from gnuradio.qtgui import qtgui
from PyQt4 import Qt
import sys
import sip

import laps_gui

#############################################################
# LaPS GUI Creation Example
#############################################################
# This shows how to use the Laps GUI package to create your 
# own grafical-based gnuradio python aplication
#############################################################

class trx(gr.top_block):
	# this variable turns on/off the GUI
	# OBS: you have to define it, laps_gui will search for it
	enable_gui = True
	samp_rate = 32000

	def __init__(self, args):
		# just the default top Block initialization
		gr.top_block.__init__(self, "Test Gui")

		# Create a gui object
		# this object contains the functions we will need to
		# setup and run the gui application
		self.gui = laps_gui.gui("Test GUI", args, self)

		# since we have created the gui object,
		# we need get the gui layout, to add a widget on it 
		self.top_layout = self.gui.get_top_layout()

		# laps_gui defines a create_tab_widget function
		# to create a sequence of tabs.
		# for example:

		# if you want to create 3 tabs named "a", "b" and "c" just do
		# tab_names = ['a', 'b', 'c']
		# uncoment the line above and run the apllication to see the result

		# but if you want two tabs with name "a" and "b", and inside of 
		# tab "a" another two tabs with name "a-1" and "a-2", just do
		# tab_names = [ ['a', 'a-1', 'a-2'] , 'b']
		# uncoment the line above and run the apllication to see the result

		# you can nest the tabs as you wish
		tab_names = [['a',['a-1','a-1.1', 'a-1.2'],['a-2','a-2.1', 'a-2.2']],['b',['b-1','b-1.1', 'b-1.2']],'c']
		# organizing the idea
		#
		# [
		#	['a',
		# 		['a-1',
		# 			'a-1.1', 'a-1.2'],/
		# 		['a-2',
		# 			'a-2.1', 'a-2.2']
		# 	],
		# 	['b',
		# 		['b-1',
		# 			'b-1.1', 'b-1.2']
		# 	],
		# 	'c'
		# ]

		# create the tab widget
		self.gui_layout = self.gui.create_tab_widget(tab_names, self.top_layout, [])

		# after tab widget created the function returns a list with the layouts of all usable tabs.
		# in the first example given, all the tabs were usable, but in the second example the tab "a"
		# already contains another tabs, so should not be used.
		# in the third example the list of usable tabs are: [a-1.1, a-1.2, a-2.1, a-2.2, b-1.1, b-1.2, c]

		# with the tabs correctly set up you just have to create the gnuradio-flow-graph blocks
		# and gui_sink blocks, connect then all 

		a = gr.vector_source_b([1,0,1], True, 1)
		thr = gr.throttle(gr.sizeof_char, self.samp_rate)
		b = gr.null_sink(gr.sizeof_char)
		self.connect(a, thr, b)

		c2f = gr.char_to_float()
		# for qtgui block creation see http://gnuradio.org/redmine/projects/gnuradio/wiki/TutorialsQTGUI
		gui_a = qtgui.time_sink_f( 512, self.samp_rate, "", 1)
		self.connect(a, c2f, gui_a)

		# now just wrap the qtgui block to a widget and add the widget into some of the 
		# layouts returned by the create_tab_widget function
		gui_win = sip.wrapinstance(gui_a.pyqwidget(), Qt.QWidget)
		self.gui_layout[0].addWidget(gui_win)

if __name__ == '__main__':
	tb = trx(sys.argv) # Create trx object class
	tb.start() #  start gnuradio flow-graph
	tb.gui.start() # now just run gui application
	tb.stop()



